﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;

namespace Communications
{
    public class EmailSender
    {
        public string FromEmailAddress = "";  //something like "yourmamma@gmail.com"
        public string EmailServerName = "";  //something like "smtp.gmail.com"
        public List<string> RecipientEmailAddresses = new List<string>();  //something like ["you@aol.com","them@yahoo.com"]
        public string MessageSubject = "";
        public string MessageBody = "";
        public List<string> AttachmentFiles = new List<string>();
        public string FromEmailAccountUsername = "";  //clear text, should probably be encrypted
        public string FromEmailAccountPassword = "";  //clear text, should probably be encrypted
        public int SmtpPort = 587;  //default of 587, works with gmail, not sure of others

        public void SendEmail()
        {
            try
            {
                MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient(EmailServerName);
                mail.From = new MailAddress(FromEmailAddress);
                mail.Subject = MessageSubject;
                mail.Body = MessageBody;
                foreach (string RecipientEmailAddress in RecipientEmailAddresses)
                {
                    mail.To.Add(new MailAddress(RecipientEmailAddress));
                }
                foreach (string AttachmentFile in AttachmentFiles)
                {
                    mail.Attachments.Add(new System.Net.Mail.Attachment(AttachmentFile));
                }

                SmtpServer.Port = SmtpPort;
                SmtpServer.Credentials = new System.Net.NetworkCredential(FromEmailAccountUsername,FromEmailAccountPassword);
                SmtpServer.EnableSsl = true;

                SmtpServer.Send(mail);                
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}